using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class application_step3_sea_transport : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        InitializeUI();
        LoadDeparture();
        LoadSeaPort();
        LoadVesselTypes(langId);
        LoadSpecialCondTypes(langId);
        setAttributes();
        LoadCurrencyTypes(langId);
        LoadTimePeriods();
        LoadRoutes(langId);
        LoadLevelOfImportanceTypes(langId);
        LoadPlacementTypes(langId);
    }

    private void LoadTimePeriods()
    {
        ddl_time_period.Items.Clear();
        ddl_time_period.Items.Add(new ListItem(DAO.TextBaseFactory.loadText("days",this.LangId)));
        ddl_time_period.Items.Add(new ListItem(DAO.TextBaseFactory.loadText("weeks", this.LangId)));
        ddl_time_period.Items.Add(new ListItem(DAO.TextBaseFactory.loadText("months", this.LangId)));
    }

    private void LoadCurrencyTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 11);
                
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPrice.Items.Clear();
                while (dr.Read())
                {
                    ddlPrice.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadRoutes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,500,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 21);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlRute.Items.Clear();
                //ddlRute.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlRute.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlRute.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void setAttributes()
    {
        //ddlSeaPortLoading.Attributes.Add("validate", "true");
        ddlFlag.Attributes.Add("validate", "true");
        ddlVeselTypes.Attributes.Add("validate", "true");
        tbVesselBuiltDate.Attributes.Add("validate", "true");
        ddlRute.Attributes.Add("multiple", "multiple");
        //ddlLevelOfImportnce.Attributes.Add("validate", "true");
        ddlPlacementTypes.Attributes.Add("validate", "true");
        tbClass.Attributes.Add("validate", "true");
    }

    private void LoadDeparture()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetAllPlaces(0, 1000000, 'id', 'asc', 2) WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlDeparture.Items.Clear();
                ddlFlag.Items.Clear();
                ddlDeparture.Items.Add(new ListItem("", "0"));
                ddlFlag.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlDeparture.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                    ddlFlag.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadSeaPort()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            long country_load_id = 0;
            if (Request.QueryString["country_load_id"] != null)
            {
                Int64.TryParse(Request.QueryString["country_load_id"].ToString(), out country_load_id);
            }

            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetPlacesByCountry(" + country_load_id + ",1) WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlSeaPortLoading.Items.Clear();
                ddlSeaPortLoading.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlSeaPortLoading.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadVesselTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,10000,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 3);
                
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlVeselTypes.Items.Clear();
                ddlVeselTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlVeselTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlVeselTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadSpecialCondTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {

                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 5);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlSpecualConditionTypes.Items.Clear();
                ddlSpecualConditionTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlSpecualConditionTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlSpecualConditionTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadLevelOfImportanceTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 16);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlLevelOfImportnce.Items.Clear();
                ddlLevelOfImportnce.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlLevelOfImportnce.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlLevelOfImportnce.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadPlacementTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 8);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPlacementTypes.Items.Clear();
                ddlPlacementTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }


    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }
}
